<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="乡镇" prop="villagesTowns">
        <el-input
          v-model="queryParams.villagesTowns"
          placeholder="请输入乡镇"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="村庄" prop="village">
        <el-input
          v-model="queryParams.village"
          placeholder="请输入村庄"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="identityCard">
        <el-input
          v-model="queryParams.identityCard"
          placeholder="请输入身份证"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['registration:registration:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['registration:registration:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['registration:registration:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
<!--     v-hasPermi="['dgw:tagNumber:import']"    -->
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['registration:registration:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registrationList" @selection-change="handleSelectionChange">
<!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="存栏量">
              <span>{{ props.row.restockSum }}</span>
            </el-form-item>
            <el-form-item label="地址">
              <span>{{ props.row.address }}</span>
            </el-form-item>
            <el-form-item label="电话">
              <span>{{ props.row.phone }}</span>
            </el-form-item>
          </el-form>

        </template>
      </el-table-column>

      <el-table-column label="乡镇" align="center" prop="villagesTowns" width="80"/>
      <el-table-column label="村庄" align="center" prop="village" width="80" />
      <el-table-column label="场名" align="center" prop="fieldName" width="120" />
      <el-table-column label="负责人" align="center" prop="userName" />
      <el-table-column label="存栏量" align="center" prop="restockSum" />
      <el-table-column label="养殖场占地面积" align="center" prop="farmArea" />
      <el-table-column label="雨、粪、尿是否分离" align="center" prop="separation"width="100" />
      <el-table-column label="堆粪池" align="center" prop="heapCesspool" />
      <el-table-column label="尿液池" align="center" prop="urinePool" />
      <el-table-column label="身份证" align="center" prop="identityCard" width="180" />
      <el-table-column label="电话" align="center" prop="phone" width="120"/>
      <el-table-column label="地址" align="center" prop="address"width="120" />
      <el-table-column label="牛肉品种" align="center" prop="beefVar" />
      <el-table-column label="入栏时间" align="center" prop="restockTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.restockTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原产地" align="center" prop="locality" />
      <el-table-column label="购入肉牛检疫证书" align="center" prop="buyBeefCerti" />
      <el-table-column label="智能安装耳标数量" align="center" prop="earNumberSum" />
      <el-table-column label="饲料来源" align="center" prop="fodder" />
      <el-table-column label="添加剂来源" align="center" prop="additivSource" />
      <el-table-column label="口蹄疫苗检疫时间" align="center" prop="footMouthTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.footMouthTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="口蹄疫苗检疫数量" align="center" prop="footMouthSum" />
      <el-table-column label="布鲁氏菌病检疫时间" align="center" prop="brucellosisTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.brucellosisTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="布鲁氏菌病检疫数量" align="center" prop="brucellosisSum" width="100"/>
      <el-table-column label="炭疽疫苗检疫时间" align="center" prop="anthraxTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.anthraxTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="炭疽疫苗检疫数量" align="center" prop="anthraxSum" />
      <el-table-column label="兽药名称" align="center" prop="veterinaryName" width="180"/>
      <el-table-column label="兽药来源" align="center" prop="veterinaryAdditiv" />
      <el-table-column label="兽药使用时间" align="center" prop="veterinaryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.veterinaryTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dgw:RegistrationFrom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dgw:RegistrationFrom:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改档案管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="乡镇" prop="villagesTowns">
          <el-input v-model="form.villagesTowns" placeholder="请输入乡镇" />
        </el-form-item>
        <el-form-item label="村庄" prop="village">
          <el-input v-model="form.village" placeholder="请输入村庄" />
        </el-form-item>
        <el-form-item label="场名" prop="fieldName">
          <el-input v-model="form.fieldName" placeholder="请输入场名" />
        </el-form-item>
        <el-form-item label="负责人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="存栏量" prop="restockSum">
          <el-input v-model="form.restockSum" placeholder="请输入存栏量" />
        </el-form-item>
        <el-form-item label="圈舍面积" prop="colonyHouse">
          <el-input v-model="form.colonyHouse" placeholder="请输入圈舍面积" />
        </el-form-item>
        <el-form-item label="养殖场占地面积" prop="farmArea">
          <el-input v-model="form.farmArea" placeholder="请输入养殖场占地面积" />
        </el-form-item>
        <el-form-item label="雨、粪、尿是否分离" prop="separation">
          <el-input v-model="form.separation" placeholder="请输入雨、粪、尿是否分离" />
        </el-form-item>
        <el-form-item label="堆粪池" prop="heapCesspool">
          <el-input v-model="form.heapCesspool" placeholder="请输入堆粪池" />
        </el-form-item>
        <el-form-item label="尿液池" prop="urinePool">
          <el-input v-model="form.urinePool" placeholder="请输入尿液池" />
        </el-form-item>
        <el-form-item label="身份证" prop="identityCard">
          <el-input v-model="form.identityCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="牛肉品种" prop="beefVar">
          <el-input v-model="form.beefVar" placeholder="请输入牛肉品种" />
        </el-form-item>
        <el-form-item label="入栏时间" prop="restockTime">
          <el-date-picker clearable size="small"
                          v-model="form.restockTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择入栏时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原产地" prop="locality">
          <el-input v-model="form.locality" placeholder="请输入原产地" />
        </el-form-item>
        <el-form-item label="购入肉牛检疫证书" prop="buyBeefCerti">
          <el-input v-model="form.buyBeefCerti" placeholder="请输入购入肉牛检疫证书" />
        </el-form-item>
        <el-form-item label="智能安装耳标数量" prop="earNumberSum">
          <el-input v-model="form.earNumberSum" placeholder="请输入智能安装耳标数量" />
        </el-form-item>
        <el-form-item label="饲料来源" prop="fodder">
          <el-input v-model="form.fodder" placeholder="请输入饲料来源" />
        </el-form-item>
        <el-form-item label="添加剂来源" prop="additivSource">
          <el-input v-model="form.additivSource" placeholder="请输入添加剂来源" />
        </el-form-item>
        <el-form-item label="口蹄疫苗检疫时间" prop="footMouthTime">
          <el-date-picker clearable size="small"
                          v-model="form.footMouthTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择口蹄疫苗检疫时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="口蹄疫苗检疫数量" prop="footMouthSum">
          <el-input v-model="form.footMouthSum" placeholder="请输入口蹄疫苗检疫数量" />
        </el-form-item>
        <el-form-item label="布鲁氏菌病检疫时间" prop="brucellosisTime">
          <el-date-picker clearable size="small"
                          v-model="form.brucellosisTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择布鲁氏菌病检疫时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="布鲁氏菌病检疫数量" prop="brucellosisSum">
          <el-input v-model="form.brucellosisSum" placeholder="请输入布鲁氏菌病检疫数量" />
        </el-form-item>
        <el-form-item label="炭疽疫苗检疫时间" prop="anthraxTime">
          <el-date-picker clearable size="small"
                          v-model="form.anthraxTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择炭疽疫苗检疫时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="炭疽疫苗检疫数量" prop="anthraxSum">
          <el-input v-model="form.anthraxSum" placeholder="请输入炭疽疫苗检疫数量" />
        </el-form-item>
        <el-form-item label="兽药名称" prop="veterinaryName">
          <el-input v-model="form.veterinaryName" placeholder="请输入兽药名称" />
        </el-form-item>
        <el-form-item label="兽药来源" prop="veterinaryAdditiv">
          <el-input v-model="form.veterinaryAdditiv" placeholder="请输入兽药来源" />
        </el-form-item>
        <el-form-item label="兽药使用时间" prop="veterinaryTime">
          <el-date-picker clearable size="small"
                          v-model="form.veterinaryTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择兽药使用时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegistration, getRegistration, delRegistration, addRegistration, updateRegistration, exportRegistration,importTemplate } from "@/api/dgw/registration";
import { getToken } from '@/utils/auth'
export default {
  name: "Registration",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 养殖户信息登录表格数据
      registrationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        villagesTowns: null,
        village: null,
        userName: null,
        identityCard: null,
        phone: null,
      },
      // 表单参数
      form: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/dgw/registration/importData'
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询养殖户信息登录列表 */
    getList() {
      this.loading = true;
      listRegistration(this.queryParams).then(response => {
        console.log("ccc",response)
        this.registrationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        villagesTowns: null,
        village: null,
        fieldName: null,
        userName: null,
        restockSum: null,
        colonyHouse: null,
        farmArea: null,
        separation: null,
        heapCesspool: null,
        urinePool: null,
        identityCard: null,
        phone: null,
        address: null,
        beefVar: null,
        restockTime: null,
        locality: null,
        buyBeefCerti: null,
        earNumberSum: null,
        fodder: null,
        additivSource: null,
        footMouthTime: null,
        footMouthSum: null,
        brucellosisTime: null,
        brucellosisSum: null,
        anthraxTime: null,
        anthraxSum: null,
        veterinaryName: null,
        veterinaryAdditiv: null,
        veterinaryTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加养殖户信息登录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRegistration(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改养殖户信息登录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRegistration(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegistration(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除养殖户信息登录编号为"' + ids + '"的数据项？').then(function() {
        return delRegistration(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有养殖户信息登录数据项？').then(() => {
        this.exportLoading = true;
        return exportRegistration(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
